﻿Public Class Junction
    ' Index de la station de départ de la jonction
    Private _FromStation As Integer
    Public Property FromStation() As Integer
        Get
            Return _FromStation
        End Get
        Set(ByVal Value As Integer)
            _FromStation = Value
        End Set
    End Property

    ' Index de la station d'arrivée
    Private _ToStation As Integer
    Public Property ToStation() As Integer
        Get
            Return _ToStation
        End Get
        Set(ByVal Value As Integer)
            _ToStation = Value
        End Set
    End Property

    ' Points des deux lignes composant la jonction
    Private P1, P2, P3, P4 As Point

    ' Constructeur
    Sub New(FromStation As Integer, ToStation As Integer, ByRef Stations As Dictionary(Of Integer, Station))
        Me._FromStation = FromStation
        Me._ToStation = ToStation
        ' Calcul des points
        CalculateCoords(Stations)
    End Sub

    Public Sub CalculateCoords(ByRef Stations As Dictionary(Of Integer, Station))
        ' Angles entre les stations d'arrivées et de départ
        Dim A1 As Double = GetAngle(Stations.Item(FromStation), Stations.Item(ToStation))
        Dim A2 As Double = GetAngle(Stations.Item(ToStation), Stations.Item(FromStation))

        ' Première ligne séparant les deux stations
        P1 = New Point(Stations.Item(FromStation).X + Math.Cos(A1 - Math.PI / 4) * 7, _
                       Stations.Item(FromStation).Y + Math.Sin(A1 - Math.PI / 4) * 7)
        P2 = New Point(Stations.Item(ToStation).X + Math.Cos(A2 + Math.PI / 4) * 7, _
                       Stations.Item(ToStation).Y + Math.Sin(A2 + Math.PI / 4) * 7)
        ' Deuxième ligne
        P3 = New Point(Stations.Item(FromStation).X + Math.Cos(A1 + Math.PI / 4) * 7, _
                       Stations.Item(FromStation).Y + Math.Sin(A1 + Math.PI / 4) * 7)
        P4 = New Point(Stations.Item(ToStation).X + Math.Cos(A2 - Math.PI / 4) * 7, _
                       Stations.Item(ToStation).Y + Math.Sin(A2 - Math.PI / 4) * 7)
    End Sub

    Public Function GetPolygon() As Point()
        ' Retourne le rectangle représentant la jonction
        Return {P1, P2, P4, P3}
    End Function

    Public Function Intersects(P As Point) As Boolean
        ' Retourne vrai si le point P est situé dans le rectangle représentant la jonction
        Dim Shape As New Drawing2D.GraphicsPath
        Shape.AddPolygon(GetPolygon())
        Return Shape.IsVisible(P)
    End Function
End Class
